package com.gitee.zjbqdzq.entity;

import jakarta.persistence.*;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Data
@Entity
public class Album implements Serializable {

    @Transient
    private static final String regex = "[/\\\\:*?\"<>|]";

    @Id
    private Long id;
    @Column(length = 128)
    private String name;

    @OneToMany(targetEntity = Track.class, cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
    List<Track> trackList = new ArrayList<>();

    public static Album build(Tracks tracks) {
        Tracks.Data.Track first = tracks.getData().getTracks().getFirst();
        String albumTitle = first.getAlbumTitle();
        Album album = new Album();
        album.setName(albumTitle.replaceAll(regex, " "));
        album.setId(Long.valueOf(first.getAlbumId()));
        return album;
    }
}
